/** 
 * @CopyRight (c) 2012-2012
 * @Project <jtao>
 * @JDK_Version_Used:<JDK1.6>
 * @Description:TODO<描述用途>
 * @Title: WebClient.java
 * @Author: <何俊>
 * @Author_of_Contact <E:azj1001@sina.com T:18651831939>
 * @Date 2012-11-10 下午3:25:34
 * @Modified_By: <修改人>
 * @Modified_of_Contact <修改人联系方式>
 * @Modified_Date <修改日期时间>
 * @Why & What is modified <修改原因描述>
 * @Version v1.00
 */ 
package com.e.buy.web;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.commons.CommonsImpl;
import cn.commons.ICommons;
import cn.commons.stander.VerifiCodeLevel;

import com.e.buy.BuyTools;
import com.e.buy.bean.Tree;
import com.e.buy.bean.back.manager.CmiAdminUser;
import com.e.buy.bean.back.manager.LoadSystem;

/**
 * @Description:TODO(描述类别作用)
 * @author <何俊>
 * @date 2012-11-10 下午3:25:34
 * @version v1.0
 * @Other :@see #Files
 */
@Controller
public class WebClient extends BuyTools{
	
	@RequestMapping("/index.form")
	public String goIndex(HttpSession session) {
		return "index";
	}
	
	@RequestMapping("/dispatch.adm")
	public ModelAndView dispatchProject(HttpServletRequest req,HttpServletResponse rep,
			HttpSession session ,@RequestParam("url") String url,@RequestParam("p") String p) {
		CmiAdminUser user = ((CmiAdminUser) session.getAttribute("loginAdmin")).clone();
		String systemPath = user.getLoadSystem().getUrl();
		ModelAndView mv = new ModelAndView();
		//如果存在请求，则跳转请求，否则跳转jsp
		if(url.trim().length() > 0) {
			int lastIndex = url.lastIndexOf("/");
			String temp = url.substring(lastIndex);
			systemPath = systemPath + temp;
			String path = url.substring(0, lastIndex) + systemPath;
			mv.setViewName("forward:" + path);
		} else {
			if(p.endsWith(".jsp")) {
				p = p.substring(0, p.lastIndexOf(".jsp"));
			}
			mv.setViewName(p);
		}
		return mv;
	}
	
	@RequestMapping("/verifiCode.ajax")
	public void verifiCode(HttpSession session,HttpServletResponse response){
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		ICommons commonsTools = new CommonsImpl();
		
		Map<String, Object> map = commonsTools.verifiCodeMake(VerifiCodeLevel.LEVEL2.getVerLevel(),16);
		session.setAttribute("vcode", map.get("vcode"));
		try {
			ImageIO.write((BufferedImage)map.get("image"), "JPEG", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/adminFrame.form")
	public ModelAndView adminFrame(HttpSession session) {
		CmiAdminUser user = ((CmiAdminUser) session.getAttribute("loginAdmin"));
		String menus = "[]";
		String web = "frame";
		String sysPath = "";
		ModelAndView mv = new ModelAndView();
		
		try {
			//获取登录的系统
			String sql = service.getSql("sql", "admin.login.system.byid");
			LoadSystem sys = (LoadSystem) service.queryForObject(sql, LoadSystem.class, 1);
			if (sys == null) {
				throw new Exception("Laod System Is Undefiend!");
			}
			sysPath = sys.getUrl();
			user.setLoadSystem(sys);
			Object[] args = new Object[0];
			
			//获取用户可操作菜单
			if (user.getPower() == 1) {
				sql = service.getSql("sql", "admin.login.menu.all");
			} else {
				sql = service.getSql("sql", "admin.login.menu.user");
				args = new Object[]{user.getUuid()};
			}
			@SuppressWarnings("unchecked")
			List<Tree> ls = (List<Tree>) service.queryForList(sql, Tree.class,args);
			if(ls != null && !ls.isEmpty()) {
				List<Tree> supers = new ArrayList<Tree>();
				for (Tree c : ls) {
					for (Tree s : ls) {
						if (s.getId().equals(c.getSuperNm())) {
							s.getChildren().add(c);
							supers.add(c);
						}
					}
				}
				ls.removeAll(supers);
				menus = toolsbox.list2JsonArrayStr(ls);
			}
		} catch (Exception e) {
			log4j.error("Load User Power Menu Error ：" + e);
			web = "adminBack/adminExit.form";
		}
		mv.setViewName(web);
		mv.addObject("navgation", menus);
		mv.addObject("sysPath", sysPath);
		return mv;
	}
	
	@RequestMapping("/adminWelcome.adm")
	public String adminWelcome(HttpSession session) {
		//欢迎信息
		return this.getWebProperties("web", "frame.welcome");
	}
}
